package com.etalk.crm.controller;

import com.alibaba.fastjson.JSON;
import com.etalk.crm.pojo.OrderLog;
import com.etalk.crm.service.OrderLogService;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

import static com.etalk.crm.utils.Constants.DEFAULT_PAGE_NUM;
import static com.etalk.crm.utils.Constants.DEFAULT_PAGE_SIZE;

/**
 * 订单操作日志
 *
 * @author Terwer
 * @version 1.0
 * 2019/6/12 14:49
 **/
@Controller
@RequestMapping("/order/log")
public class OrderLogController {
    private final Logger logger = LogManager.getLogger(this.getClass());

    @Resource
    private OrderLogService orderLogService;

    /**
     * 日志列表
     *
     * @param pageNum  页码
     * @param pageSize 每页展示的数目
     * @return 列表
     */
    @RequestMapping("/page/list")
    public String orderList(
            Model model,
            Integer pageNum,
            Integer pageSize,
            String search
    ) {
        logger.info("获取订单操作记录列表");
        if (pageNum == null) {
            pageNum = DEFAULT_PAGE_NUM;
        }
        if (pageSize == null) {
            pageSize = DEFAULT_PAGE_SIZE;
        }

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("search", search);
        PageInfo<OrderLog> pageInfo = orderLogService.getOrderLogList(paramMap, pageNum, pageSize);
        if (pageInfo != null) {
            model.addAttribute("pageInfo", pageInfo);
            logger.info("获取到的订单操作记录列表：+" + JSON.toJSONString(pageInfo));
        }
        model.addAttribute("search", search);
        return "orderLog/order_log_list";
    }
}
